package com.yusael.controller;

import com.yusael.entity.User;
import com.yusael.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 登录方法
    @PostMapping("/login")
    public String login(String username, String password, HttpSession session) {
        User login = userService.login(username, password);
        if (login != null) {
            session.setAttribute("user", login);
            System.out.println("登录成功");
            return "redirect:/emp/findAll"; // 跳转到查询所有
        } else {
            return "redirect:/index"; // 跳转回到登录
        }
    }

    // 注册方法
    @PostMapping("/register")
    public String register(User user) {
        userService.register(user);
        System.out.println("注册成功");
        return "redirect:/index";      // 统一跳转到首页（或登录页）
    }
}
